-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
otus-04 hw #1
otus-04 hw #1
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Хорошо, но постарайтесь реализовывать более простые и лаконичные решения, и пользоваться готовыми функциями/макросами из стандартной библиотеки
(defn next-coords | ||
[next-index-col next-index-row {:keys [col row]}] | ||
{:row (next-index-row row) | ||
:col (next-index-col col)}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вопрос вкуса, можно еще (-> m (update :row next-index-row) (update :col next-index-col))
[item n] | ||
(->> item | ||
(repeat n) | ||
(vec))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно просто vec
без скобок
(defn reducer [acc val] | ||
(if (acc val) | ||
(update acc val inc) | ||
(assoc acc val 1))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Откройте для себя fnil
: (update acc val (fnil inc 0))
|
||
(defn word->map | ||
[word] | ||
(reduce reducer {} word)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Откройте для себя frequencies
:)
word-map (word->map word) | ||
word-map-keys (keys word-map)] | ||
|
||
(reduce #(if (< (letters-map %2 0) (word-map %2 0)) (not %1) %1) true word-map-keys))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Откройте для себя merge-with
Итого весь код решения
(defn scramble? [letters word]
(every? (comp not pos? second)
(merge-with -
(frequencies word)
(frequencies (filter (set word) letters)))))
new-row (assoc square-row col square-val)] | ||
|
||
(assoc acc row new-row))) | ||
empty-vec (keys square-map)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Хорошо, но многословно. Сравните, для примера, с вариантом
(defn magic-square [n]
(let [sq (->> 0 (repeat n) vec (repeat n) vec)
move (fn [sq [r c]]
(let [norm (fn [x] (cond (< x 0) (dec n) (>= x n) 0 :else x))
rc' [(-> r dec norm) (-> c inc norm)]]
(if (zero? (get-in sq rc'))
rc'
[(-> r inc norm) c])))
rc-start [0 (quot n 2)]]
(loop [rc rc-start
[i & is] (range 2 (inc (* n n)))
sq (assoc-in sq rc-start 1)]
(if-not i
sq
(let [rc' (move sq rc)]
(recur rc' is (assoc-in sq rc' i)))))))
|
||
;; Logic | ||
(defn- get-idx [i] | ||
(Math/floor (/ i alphabet-size))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quot
No description provided.